using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using Voodoo.Sauce.Common.Utils;
using Voodoo.Sauce.Internal.CrossPromo.Mobile;
using Voodoo.Sauce.Internal.CrossPromo.Models;

namespace Voodoo.Sauce.Internal.CrossPromo.Canvas
{
	internal abstract class AFormatManager : MonoBehaviour
	{
		public static AFormatManager Instance;

		protected AssetModel Asset;

		protected bool IsActive;

		protected bool IsWaiting;

		private const string ITUNES_URL = "https://itunes.apple.com/fr/app/apple-store/id{0}";

		private const string PLAYSTORE_URL = "https://play.google.com/store/apps/details?id={0}";

		public abstract void Display();

		public virtual void OnClickEvent()
		{
			if (Asset?.game != null)
			{
				if (string.IsNullOrEmpty(Asset.store_ios_url))
				{
					SendTrackingLink(Asset.tracking_link);
					OpenAppStoreUsingId(Asset.game.apple_id, Asset.game.bundle_id);
				}
				else
				{
					OpenAppStoreUsingUrl(Asset.store_ios_url);
				}
				CrossPromoEvents.TriggerClickEvent(Asset);
			}
		}

		private void SendTrackingLink(string trackingLink)
		{
			if (!PlatformUtils.UNITY_EDITOR && !string.IsNullOrEmpty(trackingLink))
			{
				UnityWebRequest.Get(trackingLink).SendWebRequest();
			}
		}

		private void OpenAppStoreUsingId(long appleId, string bundleId)
		{
			if (PlatformUtils.UNITY_IOS)
			{
				if (PlatformUtils.UNITY_EDITOR)
				{
					Application.OpenURL($"https://itunes.apple.com/fr/app/apple-store/id{appleId}");
				}
				else
				{
					if (!IOSCrossPromoWrapper.CheckGoodIosVersion())
					{
						IOSCrossPromoWrapper.LoadNativeStore(appleId);
					}
					IOSCrossPromoWrapper.OpenNativeStore(appleId);
				}
			}
			if (PlatformUtils.UNITY_ANDROID)
			{
				Application.OpenURL($"https://play.google.com/store/apps/details?id={bundleId}");
			}
		}

		private void OpenAppStoreUsingUrl(string url)
		{
			if (!string.IsNullOrEmpty(url))
			{
				Application.OpenURL(url);
			}
		}

		public abstract void Hide();

		protected IEnumerator WaitCrossPromoReady()
		{
			yield break;
		}
	}
}
